package lsieun.visitor;

import lsieun.cst.Constant;
import lsieun.utils.NameUtils;
import org.objectweb.asm.ClassVisitor;

import java.util.ArrayList;
import java.util.List;

public class FindInterfaceVisitor extends ClassVisitor {
    private final String interfaceName;
    public final List<String> resultList = new ArrayList<>();

    public FindInterfaceVisitor(String interfaceName) {
        super(Constant.API_VERSION);
        this.interfaceName = interfaceName;
        if (interfaceName == null || "".equals(interfaceName.trim())) {
            throw new IllegalArgumentException("interfaceName should not be empty");
        }
    }

    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        if (interfaces == null || interfaces.length == 0) return;
        for (String item : interfaces) {
            if (interfaceName.equals(item)) {
                String jarItemName = NameUtils.toJarItemName(name);
                if (!resultList.contains(jarItemName)) {
                    resultList.add(jarItemName);
                }
            }
        }
    }
}
